/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.engine;

import javax.sql.DataSource;

import org.qdss.persist.Batcher;
import org.qdss.persist.Filter;
import org.qdss.persist.PersistManager;
import org.qdss.persist.PersistManagerFactory;
import org.qdss.persist.Query;
import org.qdss.persist.dialect.Dialect;
import org.qdss.persist.engine.query.AjqlQuery;
import org.qdss.persist.engine.query.NativeQuery;
import org.qdss.persist.engine.query.NativeQueryImpl;
import org.qdss.persist.metadata.MetadataFactory;


/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Feb 10, 2009
 * @version $Id: PersistManagerFactoryImpl.java 20 2009-02-10 03:35:10Z
 *          zhaofang123@gmail.com $
 */
public class PersistManagerFactoryImpl implements PersistManagerFactory {
	private static final long serialVersionUID = -3863550450762257322L;

	final private DataSource dataSource;
	
	final private Dialect dialect;
	final private MetadataFactory metadataFactory;
	final private SQLExecutorContext executorContext;
	
	public PersistManagerFactoryImpl(DataSource dataSource, Dialect dialect,
			MetadataFactory metadataFactory) {
		this.dataSource = dataSource;
		this.dialect = dialect;
		
		this.metadataFactory = metadataFactory;
		this.executorContext = new SQLExecutorContext(metadataFactory, dialect, dataSource);
		
		ComplexFieldSQLs.install(metadataFactory, dialect);
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public Dialect getDialect() {
		return dialect;
	}

	public MetadataFactory getMetadataFactory() {
		return metadataFactory;
	}

	public SQLExecutorContext getSQLExecutorContext() {
		return executorContext;
	}
	
	public PersistManager createPersistManager() {
		return ( new PersistManagerImpl(this) );
	}
	
	public Query createQuery(String ajql) {
		return createQuery(ajql, null);
	}
	
	public Query createQuery(String ajql, Filter filter) {
		return ( new AjqlQuery(ajql, this, filter) );
	}
	
	public NativeQuery createNativeQuery(String sql) {
		return ( new NativeQueryImpl(sql, this) );
	}
	
	public Batcher createBatcher() {
		// TODO Auto-generated method stub
		return null;
	}
}
